#region using ...
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Text;
using System.Drawing.Imaging;
using System.IO;
using System.Xml;
using System.Configuration;
using System.Diagnostics;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Security.AccessControl;
using System.Runtime.InteropServices;
#endregion

namespace BugSnapper
{
    public class MainForm : Form
    {
        #region Designer Code
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
            this.btnLeftSwitch = new System.Windows.Forms.Button();
            this.timer2 = new System.Windows.Forms.Timer(this.components);
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.btnRightSwitch = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // btnLeftSwitch
            // 
            this.btnLeftSwitch.BackColor = System.Drawing.Color.Transparent;
            this.btnLeftSwitch.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnLeftSwitch.ForeColor = System.Drawing.Color.Black;
            this.btnLeftSwitch.Location = new System.Drawing.Point(12, 12);
            this.btnLeftSwitch.Name = "btnLeftSwitch";
            this.btnLeftSwitch.Size = new System.Drawing.Size(26, 34);
            this.btnLeftSwitch.TabIndex = 0;
            this.btnLeftSwitch.Text = "<";
            this.btnLeftSwitch.UseVisualStyleBackColor = false;
            this.btnLeftSwitch.Click += new System.EventHandler(this.btnSwitch_Click);
            this.btnLeftSwitch.MouseHover += new System.EventHandler(this.btnLeftSwitch_MouseHover);
            // 
            // timer2
            // 
            this.timer2.Interval = 20;
            this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
            // 
            // btnRightSwitch
            // 
            this.btnRightSwitch.BackColor = System.Drawing.Color.Transparent;
            this.btnRightSwitch.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnRightSwitch.ForeColor = System.Drawing.Color.Black;
            this.btnRightSwitch.Location = new System.Drawing.Point(1040, 12);
            this.btnRightSwitch.Name = "btnRightSwitch";
            this.btnRightSwitch.Size = new System.Drawing.Size(26, 34);
            this.btnRightSwitch.TabIndex = 1;
            this.btnRightSwitch.Text = ">";
            this.btnRightSwitch.UseVisualStyleBackColor = false;
            this.btnRightSwitch.Click += new System.EventHandler(this.btnSwitch_Click);
            this.btnRightSwitch.MouseHover += new System.EventHandler(this.btnRightSwitch_MouseHover);
            // 
            // MainForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(1078, 744);
            this.Controls.Add(this.btnRightSwitch);
            this.Controls.Add(this.btnLeftSwitch);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.Name = "MainForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Bug Snapper";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Shown += new System.EventHandler(this.MainForm_Shown);
            this.ClientSizeChanged += new System.EventHandler(this.MainForm_ClientSizeChanged);
            this.Enter += new System.EventHandler(this.MainForm_Enter);
            this.ResumeLayout(false);

        }
        #endregion
        #endregion

        #region Constructor
        public MainForm()
        {
            //
            // Required for Windows Form Designer support
            //
            this.InitializeComponent();
            #region ucBugSnapper
            this.ucBugSnapper.Dock = DockStyle.Fill;
            this.Controls.Add(this.ucBugSnapper);
            this.Controls.Add(this.ucBugRecorder);
            this.ucBugRecorder.Dock = DockStyle.Fill;
            #endregion

            #region Event Binding
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
            this.Timer1.Tick += new EventHandler(Timer1_Tick);
            this.Load += new EventHandler(MainForm_Load);
            #endregion

            #region Logging and Diagnostics
            this.Logger = new Logger();
            this.Timer1.Interval = 1000;
            #endregion

            this.InitializeSysTrayDetails();
        }
        #endregion

        #region Variables, Properties and Delegates
        ucBugSnapper ucBugSnapper = new ucBugSnapper();
        ucBugRecorder ucBugRecorder = new ucBugRecorder();
        private Logger Logger { get; set; }
        private delegate void GenericWindowsEventsHandler<TArgs>(object sender, TArgs e);
        private delegate void GenericHandler();
        private NotifyIcon trayIcon;
        private ContextMenu ucBugSnapperTrayMenu;
        private ContextMenu ucBugRecorderTrayMenu;
        private bool _isSnapping;
        private Button btnLeftSwitch;
        private Timer timer2;
        private IContainer components;
        private ToolTip toolTip1;
        private Button btnRightSwitch;

        public bool IsSnapping
        {
            get
            {
                return _isSnapping;
            }
            set
            {
                _isSnapping = value;
                if (value)
                {
                    if (this.ucBugSnapperTrayMenu.MenuItems.ContainsKey("mnuStartSnapping"))
                    {
                        this.ucBugSnapperTrayMenu.MenuItems["mnuStartSnapping"].Enabled = false;
                    }
                    if (this.ucBugSnapperTrayMenu.MenuItems.ContainsKey("mnuPauseSnapping"))
                    {
                        this.ucBugSnapperTrayMenu.MenuItems["mnuPauseSnapping"].Enabled = true;
                    }
                    if (this.ucBugSnapperTrayMenu.MenuItems.ContainsKey("mnuStopSnapping"))
                    {
                        this.ucBugSnapperTrayMenu.MenuItems["mnuStopSnapping"].Enabled = true;
                    }
                }
                else
                {
                    if (this.ucBugSnapperTrayMenu.MenuItems.ContainsKey("mnuStartSnapping"))
                    {
                        this.ucBugSnapperTrayMenu.MenuItems["mnuStartSnapping"].Enabled = true;
                    }
                    if (this.ucBugSnapperTrayMenu.MenuItems.ContainsKey("mnuPauseSnapping"))
                    {
                        this.ucBugSnapperTrayMenu.MenuItems["mnuPauseSnapping"].Enabled = false;
                    }
                    if (this.ucBugSnapperTrayMenu.MenuItems.ContainsKey("mnuStopSnapping"))
                    {
                        this.ucBugSnapperTrayMenu.MenuItems["mnuStopSnapping"].Enabled = false;
                    }
                }
            }
        }
        Timer Timer1 = new Timer();
        #endregion

        #region Events
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        this.ucBugSnapper.PerformClosingActivities();
                        this.ucBugRecorder.PerformClosingActivities();
                        trayIcon.Visible = false;
                        trayIcon = null;
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<FormClosingEventArgs>(MainForm_FormClosing), sender, e);
                }
            }
        }



        private void SnapAndSave(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        this.ucBugSnapper.btnSnapAndSave_Click(this, e);
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(SnapAndSave), sender, e);
                }
            }
        }

        private void CloseApplication(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        this.Close();
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(CloseApplication), sender, e);
                }
            }
        }

        private void StopSnapping(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        this.ucBugSnapper.StopSnapping();
                        this.ShowKeyLessWindow(false);
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(StopSnapping), sender, e);
                }
            }
        }

        private void StartSnapping(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        this.ucBugSnapper.StartSnapping();
                        this.ShowKeyLessWindow(false);
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(StartSnapping), sender, e);
                }
            }
        }

        private void PauseBugRecorder(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        this.ucBugRecorder.btnPause_Click(this, new EventArgs());
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(StratBugRecorder), sender, e);
                }
            }
        }

        private void StratBugRecorder(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        this.ucBugRecorder.btnStart_Click(this, new EventArgs());
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(StratBugRecorder), sender, e);
                }
            }
        }

        private void StopBugRecorder(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        this.ucBugRecorder.btnStop_Click(this, new EventArgs());
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(StopBugRecorder), sender, e);
                }
            }
        }

        private void PauseSnapping(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        this.ucBugSnapper.PauseSnapping();
                        if (this.ucBugSnapper.btnPauseText == "Pause")
                        {
                            if (ucBugSnapperTrayMenu.MenuItems.ContainsKey("mnuPauseSnapping"))
                            {
                                ucBugSnapperTrayMenu.MenuItems["mnuPauseSnapping"].Text = "Pause Snapping";
                            }
                        }
                        else
                        {
                            if (ucBugSnapperTrayMenu.MenuItems.ContainsKey("mnuPauseSnapping"))
                            {
                                ucBugSnapperTrayMenu.MenuItems["mnuPauseSnapping"].Text = "Resume Snapping";
                            }
                        }
                        this.ShowKeyLessWindow(false);
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(PauseSnapping), sender, e);
                }
            }
        }
        private frmKeyLess frmKeyLess { get; set; }
        public void SetMainFormWindowState(FormWindowState value)
        {
            try
            {
                this.WindowState = value;
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }
        public Point frmKeyLessLocation { get; set; }
        public void ShowKeyLessWindow(bool forceHide)
        {
            try
            {
                if (null == this.frmKeyLess)
                {
                    this.frmKeyLess = new frmKeyLess(this.ucBugSnapper);
                }
                if (this.WindowState != FormWindowState.Maximized)
                {
                    if (AppGlobal.KeyLess && this.btnLeftSwitch.Enabled && this.IsSnapping && !forceHide)
                    {
                        this.frmKeyLess.Location = this.frmKeyLessLocation;
                        this.frmKeyLess.Show();
                        Application.DoEvents();
                        System.Threading.Thread.Sleep(250);
                    }
                    else
                    {
                        this.frmKeyLessLocation = this.frmKeyLess.Location;
                        this.frmKeyLess.Hide();
                        Application.DoEvents();
                        System.Threading.Thread.Sleep(250);
                    }
                }
                else
                {
                    this.frmKeyLess.Hide();
                    Application.DoEvents();
                    System.Threading.Thread.Sleep(250);
                }
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }

        private void trayIcon_DoubleClick(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        if (this.WindowState != FormWindowState.Maximized)
                        {
                            this.SetMainFormWindowState(FormWindowState.Maximized);
                        }
                        this.BringToFront();
                        this.Show();
                        this.Focus();
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(trayIcon_DoubleClick), sender, e);
                }
            }
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        long memoryUsage = ((System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 / 1024) / 1024);
                        //long memoryUsage = (GC.GetTotalMemory(true) / 1024);
                        if (memoryUsage > 512)
                        {
                            Timer1.Stop();
                            this.SetMainFormWindowState(FormWindowState.Maximized);
                            this.Focus();
                            this.StopSnapping(this, e);
                            Timer1.Start();
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(Timer1_Tick), sender, e);
                }
            }
        }
        public void RecordingState(bool isRecording, bool isPaused)
        {
            try
            {
                //Paused
                if (isRecording && isPaused)
                {
                    if (this.ucBugRecorderTrayMenu.MenuItems.ContainsKey("mnuStartBugRecorder"))
                    {
                        this.ucBugRecorderTrayMenu.MenuItems["mnuStartBugRecorder"].Enabled = false;
                    }
                    if (this.ucBugRecorderTrayMenu.MenuItems.ContainsKey("mnuPauseBugRecorder"))
                    {
                        this.ucBugRecorderTrayMenu.MenuItems["mnuPauseBugRecorder"].Enabled = true;
                        this.ucBugRecorderTrayMenu.MenuItems["mnuPauseBugRecorder"].Text = "Resume Bug Recorder";
                    }
                    if (this.ucBugRecorderTrayMenu.MenuItems.ContainsKey("mnuStopBugRecorder"))
                    {
                        this.ucBugRecorderTrayMenu.MenuItems["mnuStopBugRecorder"].Enabled = true;
                    }
                    this.ucBugRecorderTrayMenu.MenuItems["mnuPauseBugRecorder"].Text = "Resume Bug Recorder";
                }
                //Resumed
                if (isRecording && !isPaused)
                {
                    if (this.ucBugRecorderTrayMenu.MenuItems.ContainsKey("mnuStartBugRecorder"))
                    {
                        this.ucBugRecorderTrayMenu.MenuItems["mnuStartBugRecorder"].Enabled = false;
                    }
                    if (this.ucBugRecorderTrayMenu.MenuItems.ContainsKey("mnuPauseBugRecorder"))
                    {
                        this.ucBugRecorderTrayMenu.MenuItems["mnuPauseBugRecorder"].Enabled = true;
                        this.ucBugRecorderTrayMenu.MenuItems["mnuPauseBugRecorder"].Text = "Pause Bug Recorder";
                    }
                    if (this.ucBugRecorderTrayMenu.MenuItems.ContainsKey("mnuStopBugRecorder"))
                    {
                        this.ucBugRecorderTrayMenu.MenuItems["mnuStopBugRecorder"].Enabled = true;
                    }
                }
                //Stopped
                if (!isRecording && !isPaused)
                {
                    if (this.ucBugRecorderTrayMenu.MenuItems.ContainsKey("mnuStartBugRecorder"))
                    {
                        this.ucBugRecorderTrayMenu.MenuItems["mnuStartBugRecorder"].Enabled = true;
                    }
                    if (this.ucBugRecorderTrayMenu.MenuItems.ContainsKey("mnuStopBugRecorder"))
                    {
                        this.ucBugRecorderTrayMenu.MenuItems["mnuPauseBugRecorder"].Enabled = false;
                    }
                    if (this.ucBugRecorderTrayMenu.MenuItems.ContainsKey("mnuStopBugRecorder"))
                    {
                        this.ucBugRecorderTrayMenu.MenuItems["mnuStopBugRecorder"].Enabled = false;
                    }
                }
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }

        #endregion

        #region Methods
        public void InitializeSysTrayDetails()
        {
            try
            {
                ucBugSnapperTrayMenu = new ContextMenu();

                MenuItem mnuItemStartSnapping = new MenuItem();
                mnuItemStartSnapping.Click += new EventHandler(StartSnapping);
                mnuItemStartSnapping.Name = "mnuStartSnapping";
                mnuItemStartSnapping.Text = "Start Snapping";
                ucBugSnapperTrayMenu.MenuItems.Add(mnuItemStartSnapping);

                MenuItem mnuItemPauseSnapping = new MenuItem();
                mnuItemPauseSnapping.Click += new EventHandler(PauseSnapping);
                mnuItemPauseSnapping.Name = "mnuPauseSnapping";
                mnuItemPauseSnapping.Text = "Pause Snapping";
                #region Comment
                //mnuItemPauseSnapping.OwnerDraw = true;
                //mnuItemPauseSnapping.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.DrawMenuItemCopy);
                //mnuItemPauseSnapping.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.MeasureItemMenuItemCopy);
                #endregion
                ucBugSnapperTrayMenu.MenuItems.Add(mnuItemPauseSnapping);

                MenuItem mnuItemStopSnapping = new MenuItem();
                mnuItemStopSnapping.Click += new EventHandler(StopSnapping);
                mnuItemStopSnapping.Name = "mnuStopSnapping";
                mnuItemStopSnapping.Text = "Stop Snapping";
                ucBugSnapperTrayMenu.MenuItems.Add(mnuItemStopSnapping);

                MenuItem mnuItemSnapAndSave = new MenuItem();
                mnuItemSnapAndSave.Click += new EventHandler(SnapAndSave);
                mnuItemSnapAndSave.Name = "mnuSnapAndSave";
                mnuItemSnapAndSave.Text = "Snap And Save";
                ucBugSnapperTrayMenu.MenuItems.Add(mnuItemSnapAndSave);

                MenuItem mnuItemOpenApplication = new MenuItem();
                mnuItemOpenApplication.Click += new EventHandler(trayIcon_DoubleClick);
                mnuItemOpenApplication.Name = "mnuOpenApplication";
                mnuItemOpenApplication.Text = "Open Bug Snapper";
                ucBugSnapperTrayMenu.MenuItems.Add(mnuItemOpenApplication);

                MenuItem mnuItemAboutUsApplication = new MenuItem();
                mnuItemAboutUsApplication.Click += new EventHandler(this.AboutUs);
                mnuItemAboutUsApplication.Name = "mnuAboutUs";
                mnuItemAboutUsApplication.Text = "About Us";
                ucBugSnapperTrayMenu.MenuItems.Add(mnuItemAboutUsApplication);
                
                MenuItem mnuItemExitApplication = new MenuItem();
                mnuItemExitApplication.Click += new EventHandler(CloseApplication);
                mnuItemExitApplication.Name = "mnuExitApplication";
                mnuItemExitApplication.Text = "Exit Bug Snapper";
                ucBugSnapperTrayMenu.MenuItems.Add(mnuItemExitApplication);


                #region Bug Recorder Sys Tray Menu
                this.ucBugRecorderTrayMenu = new System.Windows.Forms.ContextMenu();


                MenuItem mnuItemStartBugRecorder = new MenuItem();
                mnuItemStartBugRecorder.Click += new EventHandler(StratBugRecorder);
                mnuItemStartBugRecorder.Name = "mnuStartBugRecorder";
                mnuItemStartBugRecorder.Text = "Start Bug Recorder";
                ucBugRecorderTrayMenu.MenuItems.Add(mnuItemStartBugRecorder);

                MenuItem mnuItemPauseBugRecorder = new MenuItem();
                mnuItemPauseBugRecorder.Click += new EventHandler(PauseBugRecorder);
                mnuItemPauseBugRecorder.Name = "mnuPauseBugRecorder";
                mnuItemPauseBugRecorder.Text = "Pause Bug Recorder";
                ucBugRecorderTrayMenu.MenuItems.Add(mnuItemPauseBugRecorder);

                MenuItem mnuItemStopBugRecorder = new MenuItem();
                mnuItemStopBugRecorder.Click += new EventHandler(StopBugRecorder);
                mnuItemStopBugRecorder.Name = "mnuStopBugRecorder";
                mnuItemStopBugRecorder.Text = "Stop Bug Recorder";
                ucBugRecorderTrayMenu.MenuItems.Add(mnuItemStopBugRecorder);

                MenuItem mnuItemOpenBugRecorder = new MenuItem();
                mnuItemOpenBugRecorder.Click += new EventHandler(trayIcon_DoubleClick);
                mnuItemOpenBugRecorder.Name = "mnuOpenBugRecorder";
                mnuItemOpenBugRecorder.Text = "Open Bug Recorder";
                this.ucBugRecorderTrayMenu.MenuItems.Add(mnuItemOpenBugRecorder);

                MenuItem mnuItemAboutUsApplicationBugRecorder = new MenuItem();
                mnuItemAboutUsApplicationBugRecorder.Click += new EventHandler(this.AboutUs);
                mnuItemAboutUsApplicationBugRecorder.Name = "mnuAboutUsBugRecorder";
                mnuItemAboutUsApplicationBugRecorder.Text = "About Us";
                ucBugRecorderTrayMenu.MenuItems.Add(mnuItemAboutUsApplicationBugRecorder);

                MenuItem mnuItemExitBugRecorder = new MenuItem();
                mnuItemExitBugRecorder.Click += new EventHandler(CloseApplication);
                mnuItemExitBugRecorder.Name = "mnuExitApplicationBugRecorder";
                mnuItemExitBugRecorder.Text = "Exit Bug Recorder";
                ucBugRecorderTrayMenu.MenuItems.Add(mnuItemExitBugRecorder);
                #endregion

                // Create a tray icon. In this example we use a
                // standard system icon for simplicity, but you
                // can of course use your own custom icon too.
                trayIcon = new NotifyIcon();
                trayIcon.Text = "Bug Snapper";
                trayIcon.Icon = new Icon(this.Icon, 40, 40);
                // Add menu to tray icon and show it.
                trayIcon.ContextMenu = ucBugSnapperTrayMenu;
                trayIcon.Visible = true;
                trayIcon.DoubleClick += new EventHandler(trayIcon_DoubleClick);
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }

        }

        private void MainForm_Enter(object sender, EventArgs e)
        {
            try
            {
                this.SetMainFormWindowState(FormWindowState.Maximized);
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }


        #endregion

        private void MainForm_Shown(object sender, EventArgs e)
        {
            try
            {
                this.SetMainFormWindowState(FormWindowState.Minimized);
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            try
            {
                if (this.btnLeftSwitch.Enabled)
                {
                    this.ucBugSnapper.DisableTimer();
                    this.ucBugSnapper.Dock = DockStyle.None;
                    if (this.ucBugRecorder.Left > this.ucBugSnapperLeft)
                    {
                        this.ucBugRecorder.Left -= this.timer2.Interval;
                        this.ucBugSnapper.Left -= this.timer2.Interval;
                    }
                    else
                    {
                        this.ucBugSnapper.Left = this.ucBugRecorderLeft * -1;
                        this.ucBugRecorder.Left = this.ucBugSnapperLeft;
                        this.timer2.Enabled = false;
                        this.btnLeftSwitch.Enabled = false;
                        this.btnRightSwitch.Enabled = true;
                        this.ucBugRecorder.Dock = DockStyle.Fill;
                        this.trayIcon.Text = "Bug Recorder";
                        this.trayIcon.ContextMenu = this.ucBugRecorderTrayMenu;
                        return;
                    }
                }
                if (this.btnRightSwitch.Enabled)
                {
                    this.ucBugRecorder.Dock = DockStyle.None;
                    if (this.ucBugRecorder.Left < this.ucBugRecorderLeft)
                    {
                        this.ucBugRecorder.Left += this.timer2.Interval;
                        this.ucBugSnapper.Left += this.timer2.Interval;
                    }
                    else
                    {
                        this.ucBugSnapper.Left = this.ucBugSnapperLeft;
                        this.ucBugRecorder.Left = this.ucBugRecorderLeft;
                        this.timer2.Enabled = false;
                        this.btnLeftSwitch.Enabled = true;
                        this.btnRightSwitch.Enabled = false;
                        this.ucBugSnapper.Dock = DockStyle.Fill;
                        this.trayIcon.Text = "Bug Snapper";
                        this.trayIcon.ContextMenu = this.ucBugRecorderTrayMenu;
                        this.ucBugSnapper.EnableTimer();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }

        int ucBugSnapperLeft;
        int ucBugRecorderLeft;
        private void MainForm_Load(object sender, EventArgs e)
        {
            if (!IsDisposed)
            {
                if (!InvokeRequired)
                {
                    try
                    {
                        this.Timer1.Start();
                        Size ucBugSnapperSize = this.ucBugSnapper.Size;
                        Point ucBugSnapperLocation = new Point(0, 0);
                        this.ucBugSnapper.Dock = DockStyle.None;
                        this.ucBugSnapper.Size = ucBugSnapperSize;
                        this.ucBugSnapper.Location = ucBugSnapperLocation;
                        this.ucBugRecorder.Left = this.ucBugSnapper.Right;
                        this.ucBugRecorder.Size = ucBugSnapperSize;
                        this.ucBugRecorder.Dock = DockStyle.None;
                        this.ucBugSnapperLeft = 0;
                        this.ucBugRecorderLeft = this.ucBugRecorder.Left;
                        this.btnLeftSwitch.Location = new Point(this.ucBugSnapperLeft, this.Height / 2);
                        this.btnRightSwitch.Location = new Point(this.ucBugRecorderLeft - this.btnRightSwitch.Width, this.Height / 2);
                        this.btnRightSwitch.Enabled = false;
                        this.RecordingState(false, false);
                        this.ucBugSnapper.InitializeBugSnapper();
                    }
                    catch (Exception ex)
                    {
                        this.Logger.ProcessError(ex);
                    }
                }
                else
                {
                    Invoke(new GenericWindowsEventsHandler<EventArgs>(Timer1_Tick), sender, e);
                }
            }
        }

        private void btnLeftSwitch_MouseHover(object sender, EventArgs e)
        {
            try
            {
                this.toolTip1.SetToolTip(btnLeftSwitch, "Bug Recorder");
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }

        private void btnSwitch_Click(object sender, EventArgs e)
        {
            try
            {
                this.timer2.Enabled = true;
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }

        }

        private void btnRightSwitch_MouseHover(object sender, EventArgs e)
        {
            try
            {
                this.toolTip1.SetToolTip(btnRightSwitch, "Bug Snapper");
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }

        private void MainForm_ClientSizeChanged(object sender, EventArgs e)
        {
            try
            {
                this.ShowKeyLessWindow(false);
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }

        private void AboutUs(object sender, EventArgs e)
        {
            try
            {
                this.ucBugSnapper.btnAboutUs_Click(sender, e);
            }
            catch (Exception ex)
            {
                this.Logger.ProcessError(ex);
            }
        }
    }
}
